package logic

import (
	"context"
	"encoding/json"
	"github.com/beego/beego/v2/client/orm"
	"math"
	"rpc/model"
	"strconv"

	"rpc/internal/svc"
	"rpc/pb/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMeesageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMeesageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMeesageLogic {
	return &GetMeesageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetMeesageLogic) GetMeesage(in *rpc.Request) (*rpc.Response, error) {
	// todo: add your logic here and delete this line
	u := []model.Goods{}
	o := orm.NewOrm()
	page := 0
	num, _ := o.QueryTable("goods").Count()
	o.QueryTable("goods").Offset(page).Limit(3).All(&u)
	lastpage := math.Ceil(float64(num) / float64(3))
	res, err := json.Marshal(u)
	if err != nil {
		return nil, err
	}
	return &rpc.Response{
		Toltal:      strconv.Itoa(int(num)),
		Perpage:     "3",
		Currentpage: strconv.Itoa(page + 1),
		Lastpage:    strconv.Itoa(int(lastpage)),
		Data:        string(res),
	}, nil
}
